package com.mofang.powerdekorhelper.persenter;

import com.mofang.powerdekorhelper.base.BasePresent;
import com.mofang.powerdekorhelper.model.Product;
import com.mofang.powerdekorhelper.utils.Constants;
import com.mofang.powerdekorhelper.utils.UrlTools;
import com.mofang.powerdekorhelper.utils.http.InitRetrofit;
import com.mofang.powerdekorhelper.utils.http.RetrofitSerives;
import com.mofang.powerdekorhelper.view.ProductListView;

import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by luofei on 2017/8/24 0024.
 */

public class ProductListPersenter extends BasePresent<ProductListView> {

    Call<Product> productCall;
    RetrofitSerives retrofitSerives;

    public void getProductList(String jsonParams) {

        view.showProgress();
        retrofitSerives = InitRetrofit.getInstance().getRetrofit(UrlTools.Services);
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"), jsonParams);
        productCall = retrofitSerives.getProductList(body);
        productCall.enqueue(new Callback<Product>() {
            @Override
            public void onResponse(Call<Product> call, Response<Product> response) {
                if (response.isSuccessful()) {
                    view.setProductList(response.body());
                } else {
                    view.onError(Constants.REQUEST_ERROR_INFO);
                }
            }

            @Override
            public void onFailure(Call<Product> call, Throwable t) {
                view.onError(Constants.REQUEST_ERROR_INFO);
            }
        });
    }

    private String getParams() {
        String jsonParams = null;
        return jsonParams;
    }


    @Override
    public void stopRequest() {
        if (productCall != null) {
            productCall.cancel();
        }
    }
}
